package com.study.modules.saas.controller;

import cn.hutool.core.util.ObjectUtil;
import com.study.Util.CommonResponse;
import com.study.common.constants.RedisCache;
import com.study.common.constants.BaseConstants;
import com.study.log.event.LoginEvent;
import com.study.log.event.LogoutEvent;
import com.study.modules.saas.service.AsyncService;
import com.study.modules.saas.service.UserService;
import com.study.security.domain.CurrentUser;
import com.study.vo.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api("登录管理")
@RestController
@RequestMapping("/sa")
public class SaasLoginController {

    @Autowired
    RedisCache redisCache;

    @Autowired
    AsyncService asyncService;

    @Autowired
    UserService userService;

    @Autowired
    ApplicationContext applicationContext;

    @ApiOperation("登录")
    @PostMapping("/login")
    public CommonResponse login(@Validated @RequestBody RequestUser requestUser, HttpServletRequest request){
        CurrentUser longin = userService.longin(requestUser.getUserName(), requestUser.getPassWord());
        if (ObjectUtil.isNotNull(longin)) {
            LoginEvent event = new LoginEvent();
            event.setRequest(request);
            event.setToken(longin.getToken());
            event.setUserUuid(longin.getUuid());
            applicationContext.publishEvent(event);

            Map<String, Object> map = new HashMap<>();
            map.put("uuid", longin.getUuid());
            map.put("username", longin.getUsername());
            map.put("token", longin.getToken());

            return CommonResponse.success(map);
        }
        return CommonResponse.fail(null, null, "登录失败");
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public CommonResponse register(@Validated @RequestBody RequestUser requestUser) {
        log.info("注册开始");
        userService.register(requestUser.getUserName(), requestUser.getPassWord());
        return CommonResponse.success();
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public CommonResponse logout(@RequestHeader(BaseConstants.TOKEN) String token, HttpServletRequest request) {
        CurrentUser currentUser = userService.logOut(token);
        if (currentUser != null) {
            LogoutEvent event = new LogoutEvent();
            event.setRequest(request);
            event.setToken(token);
            event.setUserUuid(currentUser.getUuid());
            applicationContext.publishEvent(event);
            return CommonResponse.success(currentUser);
        }
        return CommonResponse.success();
    }
}
